//
//  Address_Singleton.m
//  ILovepostcard
//
//  Created by 吴琎￼ on 12-8-7.
//  Copyright (c) 2012年 开趣. All rights reserved.
//

#import "Address_Singleton.h"
#import "Address_Singleton.h"
#import "DataItem.h"
#import "FMResultSet.h"
#import "FMDatabase.h"

static Address_Singleton *address_SingletonInstance;

@implementation Address_Singleton

+(Address_Singleton *)sharedAddress_Singleton
{
    if (address_SingletonInstance == nil)
    {
        address_SingletonInstance = [[Address_Singleton alloc] init];
    }
    return address_SingletonInstance;
}

-(id)init
{
    if (self == [super init])
    {        
        self.provinceArray = [[NSMutableArray alloc] init];
        self.cityArray     = [[NSMutableArray alloc] init];
        self.countyArray   = [[NSMutableArray alloc] init];
        self.postcodeArray = [[NSMutableArray alloc] init];
        self.dataArray     = [[NSMutableArray alloc] init];        
    }
    return self;
}

- (void)getAddressData
{
    [self saveAddressLib];
    [self getProvince];
    [self getCity];
    [self getCountryAndPostcode];
}

//保存地址字典

- (void)saveAddressLib
{
    NSMutableArray *dataArray = [Address_Singleton sharedAddress_Singleton].dataArray;
    NSString *path = [[NSBundle mainBundle] pathForResource:@"mydatabase" ofType:@"sqlite"];
    
    FMDatabase *db = [FMDatabase databaseWithPath:path];
    if (![db open])
    {
        NSLog(@"Could not open db.");
    }
    
    FMResultSet *rs = [db executeQuery:@"SELECT * FROM PostCode"];
    
    while ([rs next])
    {
        DataItem *di = [[DataItem alloc] init];
        di.province = [rs stringForColumn:@"province"];
        di.city     = [rs stringForColumn:@"city"];
        di.county   = [rs stringForColumn:@"county"];
        di.postcode = [rs stringForColumn:@"postcode"];
        [dataArray addObject:di];
    }
    [rs close];
}

- (void)getProvince
{
    NSMutableSet *set = [NSMutableSet set];
    NSMutableArray *dataArray     = [Address_Singleton sharedAddress_Singleton].dataArray;
    NSMutableArray *provinceArray = [Address_Singleton sharedAddress_Singleton].provinceArray;
    
    for (int i = 0; i < [dataArray count]; i++)
    {
        DataItem *di = [dataArray objectAtIndex:i];
        [set addObject:di.province];
    }
    
    for (NSString *cityString in set)
    {
        [provinceArray addObject:cityString];
    }
}

- (void)getCity
{
    NSMutableArray *dataArray     = [Address_Singleton sharedAddress_Singleton].dataArray;
    NSMutableArray *provinceArray = [Address_Singleton sharedAddress_Singleton].provinceArray;
    NSMutableArray *cityArray     = [Address_Singleton sharedAddress_Singleton].cityArray;
    
    [cityArray removeAllObjects];
    
    for (int i = 0; i < [provinceArray count]; i++) {
        
        NSMutableArray *tmpCityArray = [NSMutableArray array];
        NSMutableSet *set = [NSMutableSet set];
        
        NSString *provinceStr = [provinceArray objectAtIndex:i];
        
        for (int y = 0; y < [dataArray count]; y++) {
            DataItem *di = [dataArray objectAtIndex:y];
            if ([di.province isEqualToString:provinceStr]) {
                [set addObject:di.city];
            }
        }
        for (NSString *cityString in set) {
            [tmpCityArray addObject:cityString];
        }
        [cityArray addObject:tmpCityArray];
    }
}

- (void)getCountryAndPostcode
{
    
    NSMutableArray *dataArray     = [Address_Singleton sharedAddress_Singleton].dataArray;
    
    NSMutableArray *cityArray     = [Address_Singleton sharedAddress_Singleton].cityArray;
    NSMutableArray *countryArray  = [Address_Singleton sharedAddress_Singleton].countyArray;
    
    [countryArray removeAllObjects];
    
    for (int i = 0; i < cityArray.count; i++) {
        NSMutableArray *tmpArray = [NSMutableArray array];
        NSArray *array = [cityArray objectAtIndex:i];
        
        for (int m = 0; m < array.count ; m ++) {
            
            NSMutableArray *array1 = [NSMutableArray array];
            NSMutableSet *set         = [NSMutableSet set];
            
            NSString *cityStr = [array objectAtIndex:m];
            
            for (int y = 0; y < [dataArray count]; y++) {
                
                DataItem *di = [dataArray objectAtIndex:y];
                if ([di.city isEqualToString:cityStr]) {
                    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
                    [dict setObject:di.postcode forKey:@"postcode"];
                    if (di.county != nil) {
                        [dict setObject:di.county   forKey:@"county"];
                    }
                    [set addObject:dict];
                }
            }
            for (NSMutableDictionary *dict in set) {
                [array1 addObject:dict];
            }
            [tmpArray addObject:array1];
        }
        [countryArray addObject:tmpArray];
    }
}


@end
